package com.example.demodatajapmapper.controller;

import com.example.demodatajapmapper.entity.Account;
import com.example.demodatajapmapper.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customers")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @PostMapping(value = "/{customerId}/accounts")
    @ResponseStatus(code = HttpStatus.CREATED)
    private Account save(@PathVariable Long customerId, @RequestBody Account account){
        return accountService.save(customerId, account);
    }

    @GetMapping(value = "/{customerId}/accounts")
    public Page<Account> findAll(@PathVariable Long customerId, Pageable pageable){
        return accountService.findAll(customerId, pageable);
    }

    @PutMapping(value = "/{customerId}/accounts/{accountId}")
    public ResponseEntity<Account> updateAccount(@PathVariable Long customerId, @PathVariable Long accountId, @RequestBody Account newAccount){
        return accountService.updateAccount(customerId, customerId, newAccount);
    }

    @DeleteMapping(value = "/{customerId}/accounts/{accountId}")
    public ResponseEntity<?> deleteAccount(@PathVariable Long customerId, @PathVariable Long accountId){
        return accountService.deleteAccount(customerId, accountId);
    }
}
